<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学号" prop="studentNumber">
        <el-input
          v-model="queryParams.studentNumber"
          placeholder="请输入学号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item   label="学院" prop="xyId">
        <el-select v-model="queryParams.xyId" filterable  @change="handlelistPronj" placeholder="请选择" clearable>
          <el-option
            v-for="item in proxyList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item  label="年级" prop="njId">
        <el-select v-model="queryParams.njId" filterable @change="handlelistProzy" placeholder="请选择" clearable>
          <el-option
            v-for="item in pronjList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="专业" prop="zyId">
        <el-select v-model="queryParams.zyId"  filterable @change="handlelistProbj" placeholder="请选择" clearable>
          <el-option
            v-for="item in prozyList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item   label="班级" prop="clId">
        <el-select v-model="queryParams.clId" filterable placeholder="请选择"
         clearable>
          <el-option
            v-for="item in probjList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-form-item>
        <el-upload
          class="upload-demo"
          action="https://ldjy.lyu.edu.cn:8078/project/theory/score/importTheoryScore"
          :before-upload="beforeUpload"
          :on-success="handleAvatarSuccess"
          :file-list="fileList"
          :list-type="'text'"
          :show-file-list="false">
          <el-button size="mini" type="success" style="margin-left: 10px;">导入</el-button>
        </el-upload>
          </el-form-item>
        <el-link href="https://ldjy.lyu.edu.cn:8078/project/theory/score/download"
                 type="warning" target="_blank" style="margin-left: 10px;" > 模板下载</el-link>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="pronoticeList">
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="学号" align="center" prop="studentNumber" />
      <el-table-column label="学院" align="center" prop="xyName" />
      <el-table-column label="年级" align="center" prop="njName" />
      <el-table-column label="专业" align="center" prop="zyName" />
      <el-table-column label="班级" align="center" prop="bjName" />
      <el-table-column label="理论成绩" align="center" prop="theoryScore" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="danger"
            round
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:pronotice:remove']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加督促管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="理论成绩" prop="theoryScore">
          <el-input v-model="form.theoryScore" placeholder="请输入需要修改的理论成绩"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定 修 改</el-button>
        <el-button @click="cancel">取 消 修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateTheoryScore,listTheoryScore } from "@/api/project/theoryScore";
import {listProbj, listPronj, listProxy, listProzy} from "@/api/project/prouser";

export default {
  name: "Pronotice",
  data() {
    return {
      // 遮罩层
      loading: true,

      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 督促数据
      pronoticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName:'',
        studentNumber:'',
        pId: null,
        njId: null,
        zyId: null,
        clId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        theoryScore:[{ required: true, message: "请输入理论成绩", trigger: "blur" }]
      },
      //督促内容
      content:'',
      fileList: [],
      theoryScore:'',
      prozyList:[],
      proxyList:[],
      proxxList:[],
      pronjList:[],
      probjList:[],
    };
  },
  mounted() {
    listProxy().then(response => {
      this.proxyList = response.rows;
    });
    // listProzy().then(response => {
    //   this.prozyList = response.rows;
    // });
    // listPronj().then(response => {
    //   this.pronjList = response.rows;
    // });
    // listProbj().then(response => {
    //   this.probjList = response.rows;
    // });
    this.getList();
  },
  methods: {

    handlelistPronj(value){
      if(value){
          console.log(value);
        var map = {};
        map.pId = value;
        listPronj(map).then(response => {
          this.pronjList = response.rows;
        });
      }
      this.pronjList = []
      this.prozyList = []
      this.probjList = []

      this.queryParams.njId = null
      this.queryParams.clId = null
      this.queryParams.zyId = null
    },
    handlelistProzy(value){
      if(value){
         var map = {};
        map.pId = value;
        listProzy(map).then(response => {
          this.prozyList = response.rows;
        });
      }

      this.prozyList = []
      this.probjList = []
      this.queryParams.clId = null
      this.queryParams.zyId = null
    },
    handlelistProbj(value){
      console.log('///',value);
      if(value){
        var map = {};
      map.pId = value;
      listProbj(map).then(response => {
        this.probjList = response.rows;
      });
      }
      this.probjList = []
      this.queryParams.clId = null

    },

    beforeUpload(file) {
      this.loading = true; // 在上传之前设置 loading 为 true，显示遮罩层
      const validExts = ['.xlsx', '.xls','.xlsm'];
      const ext = file.name.slice(file.name.lastIndexOf('.')).toLowerCase();
      if (!validExts.includes(ext)) {
        this.$message.error('只能上传 Excel 文件');
        return false;
      }
      return true;
    },

    handleAvatarSuccess(res) {
      this.loading = false; // 上传成功后设置 loading 为 false，隐藏遮罩层
      if (res.code === 200) {
        this.$message({
          type: 'success',
          message: "上传成功"
        });
      } else {
        this.$message({
          type: 'error',
          message: res.msg
        });
      }
      this.getList()
    },

    /** 查询成绩列表 */
    getList() {
      this.loading = true;
      listTheoryScore(this.queryParams).then(response => {
        this.pronoticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        theoryScore: null,
        id : null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.pronjList = []
      this.prozyList = []
      this.probjList = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 理论成绩修改操作 */
    handleUpdate(row) {
      this.reset();
      this.form.theoryScore = row.theoryScore
      this.form.id = row.id
      this.open = true;
      this.title = "理论成绩修改";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            updateTheoryScore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
      });
    }
  }
};
</script>
